package com.tinycloud.miracll.product.service.impl;

import com.tinycloud.common.core.utils.DateUtils;
import com.tinycloud.common.core.utils.StringUtils;

import com.tinycloud.common.datasource.annotation.Master;
import com.tinycloud.common.datasource.annotation.Slave1;
import com.tinycloud.miracll.product.domain.ErpOrderProducInfo;
import com.tinycloud.miracll.product.domain.ErpProductOrderGather;
import com.tinycloud.miracll.product.mapper.ErpProductOrderGatherMapper;
import com.tinycloud.miracll.product.service.IErpProductOrderGatherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 生产订单生产数据采集主Service业务层处理
 * 
 * @author tinycloud
 * @date 2021-06-26
 */
@Service
@Slave1
public class ErpProductOrderGatherServiceImpl implements IErpProductOrderGatherService
{
    @Autowired
    private ErpProductOrderGatherMapper erpProductOrderGatherMapper;

    /**
     * 查询生产订单生产数据采集主
     * 
     * @param id 生产订单生产数据采集主ID
     * @return 生产订单生产数据采集主
     */
    @Override
    public ErpProductOrderGather selectErpProductOrderGatherById(String id)
    {
        return erpProductOrderGatherMapper.selectErpProductOrderGatherById(id);
    }

    /**
     * 查询生产订单生产数据采集主列表
     * 
     * @param erpProductOrderGather 生产订单生产数据采集主
     * @return 生产订单生产数据采集主
     */
    @Override
    public List<ErpProductOrderGather> selectErpProductOrderGatherList(ErpProductOrderGather erpProductOrderGather)
    {
        return erpProductOrderGatherMapper.selectErpProductOrderGatherList(erpProductOrderGather);
    }

    /**
     * 新增生产订单生产数据采集主
     * 
     * @param erpProductOrderGather 生产订单生产数据采集主
     * @return 结果
     */
    @Transactional
    @Override
    public int insertErpProductOrderGather(ErpProductOrderGather erpProductOrderGather)
    {
        erpProductOrderGather.setCreateTime(DateUtils.getNowDate());
        int rows = erpProductOrderGatherMapper.insertErpProductOrderGather(erpProductOrderGather);
        insertErpOrderProducInfo(erpProductOrderGather);
        return rows;
    }

    /**
     * 修改生产订单生产数据采集主
     * 
     * @param erpProductOrderGather 生产订单生产数据采集主
     * @return 结果
     */
    @Transactional
    @Override
    public int updateErpProductOrderGather(ErpProductOrderGather erpProductOrderGather)
    {
        erpProductOrderGatherMapper.deleteErpOrderProducInfoByProductorderid(erpProductOrderGather.getId());
        insertErpOrderProducInfo(erpProductOrderGather);
        return erpProductOrderGatherMapper.updateErpProductOrderGather(erpProductOrderGather);
    }

    /**
     * 批量删除生产订单生产数据采集主
     * 
     * @param ids 需要删除的生产订单生产数据采集主ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteErpProductOrderGatherByIds(String[] ids)
    {
        erpProductOrderGatherMapper.deleteErpOrderProducInfoByProductorderids(ids);
        return erpProductOrderGatherMapper.deleteErpProductOrderGatherByIds(ids);
    }

    /**
     * 删除生产订单生产数据采集主信息
     * 
     * @param id 生产订单生产数据采集主ID
     * @return 结果
     */
    @Override
    public int deleteErpProductOrderGatherById(String id)
    {
        erpProductOrderGatherMapper.deleteErpOrderProducInfoByProductorderid(id);
        return erpProductOrderGatherMapper.deleteErpProductOrderGatherById(id);
    }

    /**
     * 新增生产订单生产数据信息
     * 
     * @param erpProductOrderGather 生产订单生产数据采集主对象
     */
    public void insertErpOrderProducInfo(ErpProductOrderGather erpProductOrderGather)
    {
        List<ErpOrderProducInfo> erpOrderProducInfoList = erpProductOrderGather.getErpOrderProducInfoList();
        String id = erpProductOrderGather.getId();
        if (StringUtils.isNotNull(erpOrderProducInfoList))
        {
            List<ErpOrderProducInfo> list = new ArrayList<ErpOrderProducInfo>();
            for (ErpOrderProducInfo erpOrderProducInfo : erpOrderProducInfoList)
            {
                erpOrderProducInfo.setProductorderid(id);
                list.add(erpOrderProducInfo);
            }
            if (list.size() > 0)
            {
                erpProductOrderGatherMapper.batchErpOrderProducInfo(list);
            }
        }
    }
}
